package com.lwq.webservicecaller;

import com.alibaba.fastjson.JSONObject;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;

import javax.xml.namespace.QName;
import java.time.LocalDateTime;

/**
 * @author liwq
 * @description
 * @date 2021/11/18 9:51
 */
public class callUtils {

    public static void main(String[] args) {
        String respXML1 = callWebServiceCxf("http://localhost:8080/demo/api?wsdl", "sayHello", "123");
        System.out.println("respXML:" + respXML1);

        // String param = "<?xml version=\"1.0\" encoding=\"GBK\"?>\n" + " <FPXT_INPUT>\n" + " <ID>DLSB</ID>\n"
        // + " <SHMW>NTAwMTAyMjAzMTA5MjM3NjE1Cg==</SHMW>\n"
        // + "
        // <DATA>PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iR0JLIj8+CjxJTlBVVD4gCjxTSD41MDAxMDIyMDMxMDkyMzc2MTU8L1NIPiAgCjxaRkpIPjA8L1pGSkg+IAo8L0lOUFVUPgo=</DATA>\n"
        // + " </FPXT_INPUT>";
        // String respXML = callWebServiceCxf("http://www.qdhtxxrd.com:7788/api/plat/xxfp/service/CommService?wsdl",
        // "DLSBService", param);
        // System.out.println("respXML:" + respXML);

    }

    /**
     * cxf调用webservice服务
     * 
     * @param wsdUrl 服务地址
     * @param operationName 方法名称
     * @param params 参数
     * @return 服务响应结果
     */
    public static String callWebServiceCxf(String wsdUrl, String operationName, Object... params) {
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client client = dcf.createClient(wsdUrl);
        try {
            Object[] objects = client.invoke(operationName, params);
            return objects[0].toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

}
